import { printLog } from '@/utils/console.js'

export function formatDate (date, fmt) {
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds()
  };
  for (let k in o) {
    if (new RegExp(`(${k})`).test(fmt)) {
      let str = o[k] + '';
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
    }
  }
  return fmt;
};
  
function padLeftZero (str) {
  return ('00' + str).substr(str.length);
};

export function getWeekDate(date) {
  
  const dateOfToday = new Date(date).getTime()
  const dayOfToday = (new Date(date).getDay() + 7 - 1) % 7
  const daysOfThisWeek = Array.from(new Array(7))
    .map((_, i) => {
      printLog(i)
      const date = new Date(dateOfToday + (i - dayOfToday) * 1000 * 60 * 60 * 24)
      printLog(date)
      var month = date.getMonth() + 1
      return date.getFullYear() + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate())
  })
  printLog(daysOfThisWeek)
  return daysOfThisWeek
}

export function getLastWeekDate(date) {
  const dateOfLastday = new Date(date).getTime() - 1000 * 60 * 60 * 24 * 7
  const dayOfLastday = (new Date(date).getDay() + 7 - 1) % 7
  const daysOfLastWeek = Array.from(new Array(7))
    .map((_, i) => {
      printLog(i)
      const date = new Date(dateOfLastday + (i - dayOfLastday) * 1000 * 60 * 60 * 24)
      var month = date.getMonth() + 1
      return date.getFullYear() + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate())
  })
  printLog(daysOfLastWeek)
  return daysOfLastWeek
}

export function getDefaultWeekDate() {
  const dateOfLastday = new Date().getTime() - 1000 * 60 * 60 * 24 * 7
  const dayOfLastday = (new Date().getDay() + 7 - 1) % 7
  const daysOfLastWeek = Array.from(new Array(7))
    .map((_, i) => {
      printLog(i)
      const date = new Date(dateOfLastday + (i - dayOfLastday) * 1000 * 60 * 60 * 24)
      var month = date.getMonth() + 1
      return date.getFullYear() + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate())
  })
  printLog(daysOfLastWeek)
  return daysOfLastWeek
}


export function getMonthDate(date) {
  const monthOfToday = new Date(date).getMonth() + 1
  const yearOfToday = new Date(date).getFullYear()
  var curMonthDays = new Date(yearOfToday, monthOfToday, 0).getDate(); //0最后一天
  var firstDate = yearOfToday.toString() + '-' + (monthOfToday < 10 ? ('0' + monthOfToday) : monthOfToday) + '-01'
  var lastDate = yearOfToday.toString() + '-' + (monthOfToday < 10 ? ('0' + monthOfToday) : monthOfToday) + '-' + curMonthDays.toString()
  return [firstDate, lastDate]  
}

export function getLastMonthDate(date) {
  const monthOfToday = new Date(date).getMonth()
  const yearOfToday = monthOfToday ? new Date(date).getFullYear() : new Date(date).getFullYear() - 1
  var curMonthDays = new Date(yearOfToday, monthOfToday ? monthOfToday : 12, 0).getDate(); //0最后一天
  var month = monthOfToday ? monthOfToday : 12
  var firstDate = yearOfToday.toString() + '-' + (month < 10 ? ('0' + month) : month) + '-01'
  var lastDate = yearOfToday.toString() + '-' + (month < 10 ? ('0' + month) : month) + '-' + curMonthDays.toString()
  return [firstDate, lastDate]  
}

export function getDefaultMonthDate() {
  const monthOfToday = new Date().getMonth()
  const yearOfToday = new Date().getFullYear()
  var curMonthDays=new Date(yearOfToday, monthOfToday, 0).getDate(); //0最后一天
  var firstDate = yearOfToday.toString() + '-' + (monthOfToday < 10 ? ('0' + monthOfToday) : monthOfToday) + '-01'
  var lastDate = yearOfToday.toString() + '-' + (monthOfToday < 10 ? ('0' + monthOfToday) : monthOfToday) + '-' + curMonthDays.toString()
  return [firstDate, lastDate]  
}

export function getDaysByMonth(date) {
  const monthOfToday = new Date(date).getMonth() + 1
  const yearOfToday = new Date(date).getFullYear()
  return new Date(yearOfToday, monthOfToday, 0).getDate(); //0最后一天
}

export function getDaysByLastMonth(date) {
  const monthOfToday = new Date(date).getMonth()
  const yearOfToday = monthOfToday ? new Date(date).getFullYear() : new Date(date).getFullYear() - 1
  return new Date(yearOfToday, monthOfToday ? monthOfToday : 12, 0).getDate(); //0最后一天
}